#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

; ----------
;  Settings
; ----------

indentY = % 60
indentX = % 50
transparentColor = ff00ff
WordPath = "C:\Progs\Microsoft Office\Office12\winword.exe" /safe-mode
startDir = docs


; ------------------
;  Global variables
; ------------------
currentDir := startDir
xAppPID = ""

; --------------
;  Calculations
; --------------

workAreaH = % a_screenheight - 2 * indentY
workAreaW = % workAreaH  * 210 / 297
workAreaX = % ( a_screenwidth - workAreaW ) / 2
workAreaY = % indentY
leftW = % workAreaX - 2 * indentX
leftH = % 30
leftX = % indentX
rightX = % workAreaX + workAreaW + indentX


; ---------------------------
;  Start Application (Win+B)
; ---------------------------

#b::
{
	Gui, 2:Default
	Gui, 1:+AlwaysOnTop -Caption
	Gui, 1:Show, w%a_screenwidth% h%a_screenheight% x0 y0, Glass
	WinSet, Transparent, 5, Glass

	Gui, 2:-Caption +Owner1
		
	Gui, 2:Add, Button , vSelectButton gSelectButton x%leftX% y%indentY% w%leftW%, Select
	Gui, 2:Add, Button , vOpenButton gOpenButton x%leftX% y%indentY% w%leftW%, Open
	Gui, 2:Add, Button , vNext gNext x%rightX% y%indentY% w%leftW% h%leftH%, Prev
	yy = % leftY + leftH + indentY
	Gui, 2:Add, Button , vPrev gPrev x%rightX% y%yy% w%leftW% h%leftH%, Next
	gosub, addFileListView
	Gui, 2:Add, Picture, x%workAreaX% y%workAreaY% w%workAreaW% h%workAreaH% 0x4000000 vWorkAreaPicture, %transparentColor%.bmp

	GuiControl, 2:Hide, WorkAreaPicture
	GuiControl, 2:Hide, SelectButton	
	GuiControl, 2:Hide, Next
	GuiControl, 2:Hide, Prev
	
	Gui, 2:Show, w%a_screenwidth% h%a_screenheight% x0 y0, Main
	WinSet, TransColor, OFF, Main
	WinSet, TransColor, %transparentColor%, Main
	return
}


; -----------
;  Functions
; -----------


; *** Open Button Click ***

OpenButton:
	{
		RowNumber := LV_GetNext(0)
		if(RowNumber < 1)
		{
			return
		} 
		LV_GetText(FileName, RowNumber)
		Run, %WordPath% "%a_scriptdir%\%startDir%\%FileName%",,,xAppPID
		
		blockinput, on
		
		winwaitactive, ahk_pid %xAppPID%,,5
		;WinMinimize
		
		wt = % workAreaW ; - 100
		ht = % workAreaH + 75 ; - 100
		xt = % workAreaX ; + 50
		yt = % workAreaY - 53 ; + 50
		
		WinSet, Style, -0xC00000, A
		WinSet Redraw
		WinMove, ahk_pid %xAppPid%, , %xt%, %yt% , %wt%, %ht%	
		Send ^{F2}
		;WinMove, ahk_pid %xAppPid%, , %xt%, %yt% , %wt%, 250	
		
		;Gui, 2:Add, Text,,ErrorLevel:%ErrorLevel%`nappPID:%xAppPid% `nX:%workAreaX%--%xt%`nY:%workAreaY%--%yt%`nW:%workAreaW%--%wt%`nH:%workAreaH%--%ht%
		
		winactivate, Main

		blockinput, off

		Click 1, 1 
		
		GuiControl, 2:Hide, FileListView
		GuiControl, 2:Hide, OpenButton
		guicontrol, 2:Show, WorkAreaPicture
		GuiControl, 2:Show, SelectButton
		GuiControl, 2:Show, Next
		GuiControl, 2:Show, Prev

		return
	}

; *** Select Button Click ***
SelectButton:
	{
		GuiControl, 2:Hide, WorkAreaPicture
		GuiControl, 2:Hide, SelectButton
		GuiControl, 2:Hide, Next
		GuiControl, 2:Hide, Prev
		GuiControl, 2:Show, OpenButton
		GuiControl, 2:Show, FileListView
		;winkill, ahk_pid %xAppPID%
		process, close, %xAppPID%
		return
	}

; FileListView
addFileListView:
	{
		Gui, 2:Add, ListView, r20 x%workAreaX% y%workAreaY% w%workAreaW% gFileListView vFileListView, Name
		Loop, %startDir%\*, 1
		{
  		LV_Add("", A_LoopFileName)
		}
		LV_ModifyCol()
		LV_ModifyCol(1,"SortDesc")
		return
	}
	
setFileListView(dir):
	{
		Loop, %dir%\*, 1
		{
  		LV_Add("", A_LoopFileName)
		}
		LV_ModifyCol()
		LV_ModifyCol(1,"SortDesc")
		return
	}	
	
FileListView:
{	
	;Gui, Add, Text, x0 y0,asdfasd
	;msgbox ,%a_eventinfo%
	;RowNum = %a_eventinfo%
	;msgbox ,%RowNum%
	return
}	

Next:
	{
		blockinput, on
		winactivate, ahk_pid %xAppPID%
		Send ^!+2
		winactivate, Main
		blockinput, off
		return
	}
	
Prev:
	{
		blockinput, on
		winactivate, ahk_pid %xAppPID%
		Send ^!+1
		winactivate, Main
		blockinput, off
		return
	}	

Glass:
{
	winactivate, Main
	return
}

#r::
{
	send , ^s
	reload
	return
}